package gms;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;

import config.Configure;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


public class RedisUserHomelineLookupTable implements ILookupTable {
	IPartition partition = null;
	JedisPool pool = null;
	Jedis jedis = null;
	TokenMetaData tokenMetaData = null;

	@Override
	public void init(HashMap<String, Object> conf) {
		// TODO Auto-generated method stub
		JedisPool pool = new JedisPool(new JedisPoolConfig(),
				(String) conf.get("jedis"));
		jedis = pool.getResource();
		jedis.select((Integer) conf.get("db"));
		partition = new StringTokenPartition();
	}

	/**
	 * @param entry
	 *            : it is an String object represents the ID of an user
	 */
	@Override
	public List<InetAddress> getTarget(Object entry) {
		String key = (String) entry;
		String group = jedis.hget(key.substring(0, 4),
				key.substring(5, key.length() - 1));
		IToken token = partition.partition(group);

		return tokenMetaData.lookupNodes(token, 1);
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub

	}

}
